import { assert } from "cc";

export default class Random {
    __random(lastseed) {
        let q1 = 8039;
        let a1 = 113;
        let K = 4 * q1 + 1;
        let C = 2 * a1 + 1;
        let M = 2 ^ 16;

        let seed = lastseed % M;
        seed = K * seed + C;
        seed = seed % M;
        return { r: seed / M, seed: seed };
    }

    private _seed: number;
    private _init_seed: number;

    get init_seed(): number {
        return this._init_seed;
    }

    set seed(value: number) {
        this._init_seed = value;
        this._seed = value;
    }

    /**
     * 
     * @returns 0~1浮点数
     */
    rand(): number {
        let ret = this.__random(this._seed);
        this._seed = ret.seed;
        return ret.r;
    }

    random(n: number, m: number): number {
        assert(m >= n);

        let ret = Math.floor(this.rand() * (m + 1 - n)) + n;

        return ret;
    }
}
